import { Ref, isVNode } from "vue";
import { ComponentImp, LowCodeImp } from "../../core";

export const useMergeConfig = (
  element: Ref<ComponentImp>,
  afterGetSchemas: Map<string, any>,
  lowCode: LowCodeImp
) => {
  if (
    afterGetSchemas &&
    afterGetSchemas?.get(element.value.field) &&
    typeof afterGetSchemas.get(element.value.field) === "function"
  ) {
    const resolvedProps = afterGetSchemas.get(element.value.field)({
      formModel: lowCode.model,
      schema: element.value.componentProps,
    }); // 执行函数
    if (resolvedProps && isVNode(resolvedProps)) {
      element.value.render = resolvedProps;
      return;
    }
    Object.assign(element.value.componentProps, resolvedProps);
  } else if (
    afterGetSchemas &&
    afterGetSchemas?.get(element.value.field) &&
    typeof afterGetSchemas.get(element.value.field) === "object"
  ) {
    Object.assign(
      element.value.componentProps,
      afterGetSchemas.get(element.value.field)
    );
  } else {
    // console.log("componentProps 不是函数:", element.value.componentProps);
  }
};
